home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / vector / bitstreams.h < prev    next >
C/C++ Source or Header  |  1990-05-16  |  4KB  |  144 lines

  1. #ifndef    BITSTREAMS_H
  2. #define    BITSTREAMS_H
  3.  
  4. /* bitstreams.h -- Utility functions for manipulating bit streams
  5.  
  6.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  7.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  8.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  9.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  10.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  11.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  12.  
  13. Author:
  14.     K. E. Gorlen
  15.     Bg. 12A, Rm. 2017
  16.     Computer Systems Laboratory
  17.     Division of Computer Research and Technology
  18.     National Institutes of Health
  19.     Bethesda, Maryland 20892
  20.     Phone: (301) 496-5363
  21.     uucp: uunet!nih-csl!keith
  22.     Internet: keith%nih-csl@ncifcrf.gov
  23.  
  24. Function:
  25.     
  26. Modification History:
  27.  
  28. $Log:    bitstreams.h,v $
  29.  * Revision 3.0  90/05/16  23:00:46  kgorlen
  30.  * Release for 1st edition.
  31.  * 
  32.  * Revision 2.206  90/05/13  22:06:52  kgorlen
  33.  * Pre-release.
  34.  * 
  35.  * Revision 2.204  89/10/08  09:43:04  keith
  36.  * Pre-release
  37.  * 
  38.  * Revision 2.203  89/08/14  12:39:21  ted
  39.  * Removed register decl's
  40.  * 
  41.  * Revision 2.202  89/06/28  23:33:59  keith
  42.  * Base revision for AT&T C++ R2.0 release (Cycle 20)
  43.  * 
  44.  * Revision 2.201.1.2  89/06/28  22:45:06  keith
  45.  * Replace ambiguous operator bitVecByte*() with pt().
  46.  * 
  47.  * Revision 2.201.1.1  89/06/19  21:50:56  keith
  48.  * Base revision for R2.0 Cycle 18.
  49.  * 
  50.  * Revision 2.201  89/05/12  13:46:47  keith
  51.  * Release for R2.0 Beta test.
  52.  * 
  53.  * Revision 2.200  89/05/03  23:36:16  keith
  54.  * Utilize abstract classes.
  55.  * 
  56.  * Revision 2.122  89/05/03  23:33:27  keith
  57.  * 
  58.  * 
  59.  * Revision 2.121  89/04/25  13:33:48  keith
  60.  * Base revision for C++ R1.2.1 compatible version.
  61.  * 
  62.  * Revision 2.0  88/03/29  21:56:39  keith
  63.  * Version 2 Release 2
  64.  * 
  65.  * Revision 1.1  88/01/17  09:47:13  keith
  66.  * Initial revision
  67.  * 
  68.     
  69. */
  70. #include "BitVec.h"
  71.  
  72. class BitVecIstream : public NIHCL {
  73.     const bitVecByte* p;    // pointer to current byte
  74.     unsigned i;        // current bit index
  75. public:
  76.     BitVecIstream(const BitVec& B)    { p = B.pt();  i = 0; }
  77.     operator bool() const     { return (p[i>>3] >> (i&7)) & 1; }
  78.     void operator++()    { i++; }
  79. };
  80.  
  81. class BitVecOstream : public NIHCL {
  82.     bitVecByte* p;        // pointer to current byte
  83.     unsigned i;        // current bit index
  84.     BitVecOstream(BitVec& B)    { p = B.pt();  B = 0;  i = 0; }
  85.     void operator<<(bool b) { if (b) p[i>>3] |= 1 << (i&7);  i++; }
  86.     void operator++()    { i++; }
  87.     friend BitVec;
  88.     friend BitSlice;
  89.     friend BitPick;
  90.     friend BitSlct;
  91. };
  92.  
  93. class BitSliceIstream : public NIHCL {
  94.     bitVecByte* p;        // pointer to current byte
  95.     unsigned i;        // current bit index
  96.     int k;            // stride
  97. public:
  98.     BitSliceIstream(const BitSlice& s)    { p = s.V->pt();  i = s.pos();  k = s.stride(); }
  99.     operator bool()    { 
  100.         bool b = (p[i>>3] >> (i&7)) & 1;
  101.         i += k;
  102.         return b;
  103.     }
  104. };
  105.  
  106. class BitSliceOstream : public NIHCL {
  107.     bitVecByte* p;        // pointer to current byte
  108.     unsigned i;        // current bit index
  109.     int k;            // stride
  110.     BitSliceOstream(BitSlice& s)    { p = s.V->pt();  i = s.pos();  k = s.stride(); }
  111.     void operator<<(bool b) {
  112.         bitVecByte* q = &p[i>>3];
  113.         bitVecByte m = 1 << (i&7);
  114.         if (b) *q |= m;
  115.         else *q &= ~m;
  116.         i += k;
  117.     }
  118.     friend BitVec;
  119.     friend BitSlice;
  120.     friend BitPick;
  121.     friend BitSlct;
  122. };
  123.  
  124. class BitPickIstream : public NIHCL {
  125.     const BitVec* V;    // pointer to input BitVec
  126.     const int* p;        // pointer to current index element
  127. public:
  128.     BitPickIstream(const BitPick& s)  { V = s.V;  p = (*s.X).pt(); }
  129.     operator bool()     { return (*V)[*p++]; }
  130. };
  131.  
  132. class BitPickOstream : public NIHCL {
  133.     BitVec* V;        // pointer to input BitVec
  134.     const int* p;        // pointer to current index element
  135.     BitPickOstream(BitPick& s)    { V = s.V;  p = s.X->pt(); }
  136.     void operator<<(bool b) { (*V)[*p++] = b; }
  137.     friend BitVec;
  138.     friend BitSlice;
  139.     friend BitPick;
  140.     friend BitSlct;
  141. };
  142.  
  143. #endif
  144.